package bb.lanxing.util;

import com.github.mikephil.charting.utils.Utils;
//import com.igexin.push.config.c;
import bb.lanxing.App;
import bb.lanxing.calc.data.PowerData;
import bb.lanxing.model.database.ITrackPoint;
import bb.lanxing.model.database.User;
import java.util.List;

public class CyclingPowerUtil {
    final static private String TAG = "CyclingPowerUtil";
    public static float calcAccelerationPower(float weight, float speed, float acce) {
        return weight * speed * acce;
    }

    public static float calcGravityPower(float weight, float speed, float grade) {
        return ((((weight * 9.8f) * speed) * grade) / 100.0f) * 0.8f;
    }

    public static double calcIF(double NP, double FTP) {
        return NP / FTP;
    }

    public static float calcRollingPower(float weight, float speed, boolean isTrainingMode) {
        return weight * 9.8f * speed * (!isTrainingMode ? 0.004f : 0.025f);
    }

    public static double calcTSS(long seconds, double FTP, double NP, double IF) {
        return (((seconds * NP) * IF) / (FTP * 3600.0d)) * 100.0d;
    }

    public static double calcVI(double avgPower, double NP) {
        return NP / avgPower;
    }

    public static float coerceIn(float value, float minimumValue, float maximumValue) {
        return minimumValue > maximumValue ? value : value < minimumValue ? minimumValue : Math.min(value, maximumValue);
    }

    public static float calcRealtimePower(PowerData powerData) {
        User signinUser = App.getContext().getSigninUser();
        Log.d(TAG, "calcRealtimePower...");
        float weight = (signinUser == null || signinUser.getWeight() <= Utils.DOUBLE_EPSILON) ? 75.0f : (float) (signinUser.getWeight() + 10.0d);
        float temperature = powerData.getTemperature();
        float pressure = powerData.getPressure();
        float speed = powerData.getSpeed();
        float bearing = powerData.getBearing();
        float windSpeed = powerData.getWindSpeed();
        float windDeg = powerData.getWindDeg();
        float grade = powerData.getGrade();
        float acceleration = powerData.getAcceleration();
        boolean isTrainingMode = powerData.isTrainingMode();
        float f = 0.0f;
        float calcWindPower = isTrainingMode ? 0.0f : calcWindPower(temperature, pressure, speed, bearing, windSpeed, windDeg);
        float calcRollingPower = calcRollingPower(weight, speed, isTrainingMode);
        float calcGravityPower = isTrainingMode ? 0.0f : calcGravityPower(weight, speed, grade);
        float calcAccelerationPower = calcAccelerationPower(weight, speed, acceleration);
        Log.v(TAG, "wPower = " + calcWindPower + ", rPower = " + calcRollingPower + ", gPower = " + calcGravityPower + ", aPower = " + calcAccelerationPower);
        float f2 = calcRollingPower + calcWindPower + calcGravityPower;
        if (f2 >= 0.0f) {
            f = f2;
        }
        powerData.setPower(f);
        powerData.setWindPower(calcWindPower);
        powerData.setRollPower(calcRollingPower);
        powerData.setGravPower(calcGravityPower);
        powerData.setAccePower(calcAccelerationPower);
        Log.d(TAG, "calcRealtimePower...power=" + f);
        return f;
    }

    public static float calcWindPower(float template, float pressure, float speed, float bearing, float windSpeed, float windDeg) {
        return ((float) ((pressure > 0.0f ? (pressure * 100.0f) / ((template + 273.15f) * 287.058f) : 1.224f) * 0.5f * Math.pow((float) (speed - (windSpeed * Math.cos((360.0f - windDeg) - bearing))), 3.0d) * 0.5d * 0.5d)) * 1.5f;
    }

    public static double calcNP(List<ITrackPoint> trackPoints) {
        if (trackPoints == null || trackPoints.isEmpty()) {
            return Utils.DOUBLE_EPSILON;
        }
        long j = 0;
        int i = 0;
        int i2 = 0;
        double d = Utils.DOUBLE_EPSILON;
        double d2 = Utils.DOUBLE_EPSILON;
        for (int i3 = 0; i3 < trackPoints.size(); i3++) {
            ITrackPoint iTrackPoint = trackPoints.get(i3);
            if (iTrackPoint.getTime() - j < 30000L) { //c.k
                d += iTrackPoint.getPower();
                i++;
            } else {
                if (j > 0) {
                    d2 += Math.pow(d / i, 4.0d);
                    i2++;
                }
                j = iTrackPoint.getTime();
                i = 1;
                d = iTrackPoint.getPower();
            }
        }
        if (d > Utils.DOUBLE_EPSILON) {
            d2 += Math.pow(d / i, 4.0d);
            i2++;
        }
        return Math.pow(d2 / i2, 0.25d);
    }

    public static float estPower(double speed, long durationEach, double speedEach, float grade, float elevation) {
        Log.d(TAG, "estPower...speed=" + speed + ", durationEach=" + durationEach);
        Log.d(TAG, "estPower...speedEach=" + speedEach + ", grade=" + grade + ", elevation=" + elevation);
        if (durationEach <= 0 || speedEach <= Utils.DOUBLE_EPSILON) {
            return 0.0f;
        }
        float f = 65.0f;
        User signinUser = App.getContext().getSigninUser();
        if (signinUser != null && signinUser.getWeight() > Utils.DOUBLE_EPSILON) {
            f = (float) signinUser.getWeight();
            Log.d(TAG, "estPower...signinUser.getWeight()=" + signinUser.getWeight() + ", f=" + f);
        }
        double coerceIn = coerceIn(grade / 100.0f, -0.35f, 0.35f);
        double d = f;
        double cos = Math.cos(Math.atan(coerceIn)) * 9.8d * 0.005d * d * speed;
        double d2 = 1.293d;
        if (0.0f < elevation && elevation < 11000.0f) {
            d2 = Math.pow(((288.15d - (elevation * 0.0065d)) / 288.15d) * 1.293d, 4.25588d);
        }
        Log.d(TAG, "estPower...cos=" + cos + ", d2=" + d2);
        double pow = cos + (d2 * 0.5d * 0.82d * 0.5d * Math.pow(speed, 2.0d))
                + (d * 9.8d * speed * Math.sin(Math.atan(coerceIn))) + Utils.DOUBLE_EPSILON;
        Log.d(TAG, "estPower...pow=" + pow);
        return (float) (pow < Utils.DOUBLE_EPSILON ? 0.0d : pow);
    }
}
